package sonia.test;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

import rlm.RuleLayoutManager;
import alm.ALMException;

public class GeneralFrame extends JFrame{
	private JPanel panel;
	private ArrayList<JButton> buttons;
	private int current;
	private String layoutFile;
	
	private RuleLayoutManager layout;
	private ActionListener addAction;

	public GeneralFrame() throws ALMException{
		super("GeneralFrame");
		current = 1;
		setDefaults();
		createLayout();
		createActions();
	}

	private void setDefaults() {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.setSize(300,400);
		this.setLocation(600, 200);
	}

	public static void main(String[] args) throws ALMException{
		GeneralFrame frame = new GeneralFrame();
		
		// add components
//		for(int i = 0; i < 7; i ++)
//			frame.addButton();
		
		frame.setVisible(true);
	}
	
	private void createLayout() throws ALMException {
		layout = new RuleLayoutManager("src/GeneralFrame.drl");
		this.setLayout(layout);
		
		// add main menu
		this.setJMenuBar(new MyMenu());
		
		// add 5 buttons:
		buttons = new ArrayList<JButton>();
		for(int i = 0; i < 1; i++){
			JButton button = new JButton();
			button.setName("button"+(i+1));
			buttons.add(button);
			this.add(button);
		}
		
		// create layout file
		
		panel = new JPanel();
		panel.setName("panel");
		this.add(panel);
		panel.setLayout(new RuleLayoutManager(new FlowLayout(), "src/size.drl"));
	}
	
	public void addButton(){
		JButton newButton = new JButton(generatName());
		panel.add(newButton);
		panel.invalidate();
		panel.doLayout();
	}
	
	private void createActions(){
		addAction = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				addButton();
			}
		};
		
		buttons.get(0).addActionListener(addAction);
		//layout.addFact(addAction);
	}
	

	private String generatName(){
		String name = ""+current++;
		return name;
	}
	
	class MyMenu extends JMenuBar{
		JMenu menu;
		
		public MyMenu(){
			menu = new JMenu("File");
			menu.add(new AbstractAction(){
				{
					putValue(AbstractAction.NAME, "Load File");
					putValue(AbstractAction.SHORT_DESCRIPTION, "Load rules file (drl)");
				}
				
				@Override
				public void actionPerformed(ActionEvent e) {
					JFileChooser fc = new JFileChooser();
					int result = fc.showOpenDialog(GeneralFrame.this);
					if (result == JFileChooser.APPROVE_OPTION){
						File file = fc.getSelectedFile();
						RuleLayoutManager lm = (RuleLayoutManager) panel.getLayout();
						lm.loadFile(file.getAbsolutePath());
						
					}
				}
			});

			
			menu.add(new AbstractAction(){
				{
					putValue(AbstractAction.NAME, "Save");
					putValue(AbstractAction.SHORT_DESCRIPTION, "Save rules file (drl)");
				}

				@Override
				public void actionPerformed(ActionEvent e) {
					System.out.println("unused at the moment");
				}
			});
			
			this.add(menu);
		}
	}
}
